package indi.gtl.superbrain.activitys;

import indi.gtl.superbrain.R;
import indi.gtl.superbrain.tools.Databasehelper;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	EditText login_username_text, login_password_text;
	Button login_login_but, login_register_but;
	CheckBox login_remember_box, login_motion_box;
	Databasehelper databasehelper;
	SQLiteDatabase database;
	ActionBar actionBar;
	SharedPreferences preferences;
	Intent intent;
	Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		login_username_text = (EditText) findViewById(R.id.login_username_text);
		login_password_text = (EditText) findViewById(R.id.login_password_text);
		login_login_but = (Button) findViewById(R.id.login_login_but);
		login_register_but = (Button) findViewById(R.id.login_register_but);
		login_remember_box = (CheckBox) findViewById(R.id.login_remember_box);
		login_motion_box = (CheckBox) findViewById(R.id.login_motion_box);

		databasehelper = new Databasehelper(this);
		database = databasehelper.getWritableDatabase();
		actionBar = getActionBar();
		actionBar.hide();
		login_login_but.setOnClickListener(clickListener);
		login_register_but.setOnClickListener(clickListener);
		preferences = this.getSharedPreferences("UserInfo",
				Context.MODE_WORLD_READABLE);
		// 判断记住密码多选框的状态
		if (preferences.getBoolean("ISCHECK", false)) {
		}
		// 设置默认是记录密码状态
		login_remember_box.setChecked(true);
		login_username_text.setText(preferences.getString("name", ""));
		login_password_text.setText(preferences.getString("psw", ""));
		// 判断自动登陆多选框状态
		if (preferences.getBoolean("AUTO_ISCHECK", false)) {
			// 设置默认是自动登录状态
			login_motion_box.setChecked(true);
			// 这里差一个跳转到主界面
			Intent intent=new Intent(LoginActivity.this,MainLayoutActivity.class);
			LoginActivity.this.startActivity(intent);
			
		}
		/*
		 * 监听记住密码多选框按钮事件
		 */
		login_remember_box
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (login_remember_box.isChecked()) {
							Toast.makeText(LoginActivity.this, "记住密码已选中",
									Toast.LENGTH_SHORT).show();
							preferences.edit().putBoolean("ISCHECK", true)
									.commit();
						} else {

							Toast.makeText(LoginActivity.this, "记住密码没有选中",
									Toast.LENGTH_SHORT).show();
							preferences.edit().putBoolean("ISCHECK", false)
									.commit();

						}
					}
				});
		/*
		 * 监听自动登录多选框事件
		 */
		login_motion_box
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (login_motion_box.isChecked()) {
							Toast.makeText(LoginActivity.this, "自动登录已选中",
									Toast.LENGTH_SHORT).show();
							preferences.edit().putBoolean("AUTO_ISCHECK", true)
									.commit();
						} else {
							Toast.makeText(LoginActivity.this, "自动登录没有选中",
									Toast.LENGTH_SHORT).show();
							preferences.edit()
									.putBoolean("AUTO_ISCHECK", false).commit();
						}

					}
				});
	}

	/*
	 * 设置两个按钮的监听,并且把当前输入的账号密码和存入数据库面的账号密码做比较
	 */
	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
		
			case R.id.login_login_but:
				String landingaccount = login_username_text.getText()
						.toString();
				String loginpassword = login_password_text.getText().toString();
				// if
				// (landingaccount.equals("name")&&loginpassword.equals("psw"))
				// {
				// Toast.makeText(LoginActivity.this,"登录成功",
				// Toast.LENGTH_SHORT).show();
				// }
				if (login_remember_box.isChecked()) {
					Editor editor = preferences.edit();
					editor.putString("name", landingaccount);
					editor.putString("psw", loginpassword);
					editor.commit();
				}
				Databasehelper.Check(database, landingaccount, loginpassword,
						LoginActivity.this);

				if (landingaccount.equals("")) {
					Toast.makeText(LoginActivity.this, "登陆账号不能为空",
							Toast.LENGTH_SHORT).show();
				} else {
					if (loginpassword.equals("")) {
						Toast.makeText(LoginActivity.this, "登陆密码不能为空",
								Toast.LENGTH_SHORT).show();
					} else {
						cursor = Databasehelper.JudgeLandingaccount(database,
								landingaccount);
						if (cursor.moveToNext()) {
							String str = cursor.getString(2);
							if (str.equals(loginpassword)) {
								Toast.makeText(LoginActivity.this, "登陆成功",
										Toast.LENGTH_SHORT).show();
								Intent intent =new Intent(LoginActivity.this,MainLayoutActivity.class);
								startActivity(intent);
							} else {
								Toast.makeText(LoginActivity.this, "密码错误",
										Toast.LENGTH_SHORT).show();

							}
						} else {
							Toast.makeText(LoginActivity.this, "账号错误",
									Toast.LENGTH_SHORT).show();
						}
					}
				}
				break;

			case R.id.login_register_but:
				intent = new Intent(LoginActivity.this, RegisterActiviry.class);
				startActivity(intent);
				break;
			}
		}
	};

}
